<?php
$isReloadable = true;
require_once("../../pers/inc.config.php");
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');

if (reqVar('prefile')=='') $_REQUEST['prefile'] = uniqid('pref');

//Gestione azioni
if (reqvar('azione')=='elimina'){$msgPag .= confEli();}

// Dati: commessa selezionata
$sqlSel = " SELECT c.*, a.* ". 
          " FROM commessa c ".
	      " LEFT JOIN att_previste a ON a.id_comm = c.id_comm ".
          "                         AND a.id_rint = 0 ".
          " WHERE c.id_comm = ".intval(reqVar('id')).
          " ORDER BY a.tipo_att";
$objDB->query($sqlSel,'selRec');
if (!$acRec = $objDB->fetch('selRec')) {
	backOnErr("var. id. '".reqVar('id')."' mancante o errato");
}

// Dati: attivita' assegnate fuori rint.
$sSqlAss = "SELECT * FROM assegnaz WHERE id_comm = ".intval(reqVar('id'))." AND id_rint = 0 ".
           "ORDER BY tipo_att, id_operatore";
$objDB->query($sSqlAss,'selAss');
$aAss = array();
while ($acAss = $objDB->fetch('selAss')) {	
	$aAss[$acAss['tipo_att']][] = $acAss['id_operatore']; 
}

// Dati: richiedenti commessa.
$sSqlRich = "SELECT * FROM richiedenti WHERE id_comm = ".intval(reqVar('id'));
$objDB->query($sSqlRich,'selRich');
$aRich = array();
while ($acRich = $objDB->fetch('selRich')) {	
	$aRich[$acRich['id_operatore']] = $aUtentiRint[$acRich['id_operatore']]['nome']; 
}
asort($aRich);

// Dati: clienti commessa.
$sSqlCli = "SELECT * FROM clienti_comm WHERE id_comm = ".intval(reqVar('id'));
$objDB->query($sSqlCli,'selCli');
$aCli = array();
while ($acCli = $objDB->fetch('selCli')) {	
	$aCli[$acCli['id_cliente']] = $aClientiRint[$acCli['id_cliente']]['nome']; 
}
asort($aCli);

// array tipo rilevazione.
$aTipoRilevazione = array();
$aTipoRilevazione['R'] = 'Rint';
$aTipoRilevazione['A'] = 'Attivita\'';
$aTipoRilevazione['X'] = 'Entrambi';

$sCmdL = "";
$sCmdR = "";

if ($objSess->abilitaz['gescomm'] == 'W') {
	$sCmdR .= "<a class='cmd1' href='commMod.php?id=".reqVar('id')."&xsn=".XSN."' >modifica</a>";
	$sCmdR .= "&nbsp;&nbsp;&nbsp";
	$sCmdR .= "<a class='cmd1' href='commView.php?id=".reqVar('id')."&azione=elimina&xsn=".XSN."' >elimina</a>";
}	
$sCmdR .= cmdTorna('torna');

$aMenuPag = array();
include("_inc/inc.aMenuPag.php");
testaHtml('Visualizza commessa', $msgPag, '30.10.10', $aMenuPag);
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='top'><?=$sCmdL?></td>
      <td class="cmd1" align="right"><?=$sCmdR?></td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?php echo getHtmlRigaRif();?>
	<tr>
		<td class="tit1" colspan='20' align='center' height='25'>Scheda Commessa</td>
	</tr>
    <tr>
   	<td class="lab1" colspan='3' height='25'>Codice</td>
	  	<td class="txt1" colspan='7'><?=$acRec['cod_comm'];?></td>
   	<td class="lab1" colspan='3' height='25'>Cod. Co.Ge.</td>
	  	<td class="txt1" colspan='7'><?=$acRec['cod_coge'];?></td>
    </tr>
    <tr>
   	  	<td class="lab1" colspan='3' height='25'>Descrizione</td>
	  	<td class="txt1" colspan='17'><?=$acRec['descr'];?></td>
    </tr>
    <tr>
      	<td class="lab1" colspan='3' height='25'>Data Inizio</td>
	  	<td class="txt1" colspan='7'><?=YMD2DMY($acRec['data_iniz']);?></td>
      	<td class="lab1" colspan='3'>Data Fine</td>
	  	<td class="txt1" colspan='7'><?=YMD2DMY($acRec['data_fine']);?></td>   
    </tr>
    <tr>	  
      	<td class="lab1" colspan='3' height='25'>Responsabile</td>
	  	<td class="txt1" colspan='7'><?=$aUtentiRint[$acRec['id_responsabile']]['nome'];?></td>
	  	<td class="lab1" colspan='3'>Cliente no-rint</td>
	  	<td class="txt1" colspan='7'>
	  	<?  if (isset($aClientiRint[$acRec['id_cliente']])) {
	  			echo $aClientiRint[$acRec['id_cliente']]['nome'];
	  		} else {
	  			echo "&nbsp;";
	  		} ?>
	  	</td>  
    </tr>
    <tr>
    	<td class="lab1" colspan='3' height='25'>Tipo attivita' previste</td>
    	<td class="txt1" colspan='17'>
<? if ($objDB->numRec('selRec') > 1 or ($objDB->numRec('selRec') == 1 and !is_null($acRec['tipo_att']))) {   	
  	   		$objDB->seekFirst('selRec');
    		echo "<table class='tab1' border='0' cellpadding='0' cellspacing='0' width='100%'>";
			echo "<tr>";
    		echo "<td colspan='3' class='txt1' height='20'>&nbsp;</td>";
    		echo "<td class='lab1'>Operatori assegnati attivita' fuori rint</td>";
			echo "</tr>";
    		while ($acRec = $objDB->fetch('selRec')) {
				echo "<tr>";
				echo "<td class='txt1' width='10%'>".$aTipiAttRint[$acRec['tipo_att']]['des']."</td>";
	    		echo "<td class='txt1' width='5%'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
				echo "<td class='txt1' width='10%'> (".$aTipoRilevazione[$acRec['tipo_rilevazione']].") </td>";
				echo "<td class='txt1'>";
    			$strAss = "&nbsp;";
    			if (isset($aAss[$acRec['tipo_att']])) {
	    			foreach ($aAss[$acRec['tipo_att']] as $ope) {
	    				$strAss .= $aUtentiRint[$ope]['nome'].", "; 	
	    			}	    			
	    			if (count($aAss[$acRec['tipo_att']]) > 0) { 
	    				echo substr($strAss, 0, -2);
	    			} else {
	    				echo $strAss; 
	    			}
    			}
				echo "</td>";
				echo "</tr>";
			}
			echo "</table>";
   }
?>			
    	</td>
    </tr>
    <tr>
     	<td class="lab1" colspan='3' height='25'>Richiedenti</td>
   		<td class="txt1" colspan='17'>
		<? $strRich = "&nbsp;";
		   foreach ($aRich as $ope => $nome) {
				$strRich .= $nome.", ";
		   }
    	   if (count($aRich) > 0) { 
    			echo substr($strRich, 0, -2);
    	   } else {
    			echo $strRich; 
    	   } ?>			
		</td>
    </tr>
    <tr>
     	<td class="lab1" colspan='3' height='25'>Clienti rint</td>
   		<td class="txt1" colspan='17'>
		<? $strCli = "&nbsp;";
		   foreach ($aCli as $cli => $nome) {
				$strCli .= $nome.", ";
		   }
    	   if (count($aCli) > 0) { 
    			echo substr($strCli, 0, -2);
    	   } else {
    			echo $strCli; 
    	   } ?>			
		</td>
    </tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='top'><?=$sCmdL?></td>
      <td class="cmd1" align="right"><?=$sCmdR?></td>
   </tr>
</table>
<?
codaHtml();
exit();

function confEli() {
	global $objDB, $objSess;

	$msg = '';
	
	// verifico l' esistenza di rint aperti sulla commessa
	$sSqlRint = " SELECT count(*) as tot FROM rint WHERE id_comm = ".intval(reqVar('id'));
	$objDB->query($sSqlRint,'selRint');
	$acRint = $objDB->fetch('selRint');
	
	if ($acRint['tot'] > 0) {
		$msg .= 'eliminazione non consentita:<br>esistono rint aperti su di essa';
	}
	if ($msg != '') {return $msg;}
	
	// verifico l' esistenza di attivita' eseguite sulla commessa
	$sSqlAtt = " SELECT count(*) as tot FROM attivita WHERE id_comm = ".intval(reqVar('id'));
	$objDB->query($sSqlAtt,'selAtt');
	$acAtt = $objDB->fetch('selAtt');
	
	if ($acAtt['tot'] > 0) {
		$msg .= 'eliminazione non consentita:<br>esistono attivita\' eseguite su di essa';
	}
	if ($msg != '') {return $msg;}
	
	// eliminazione record dalla tabella commessa e 
	// di tutti i record di tutte le tabelle in cui la commessa e' stata inserita.
	$sDelComm = "DELETE FROM commessa WHERE id_comm = ".intval(reqVar('id'));
	$objDB->exec($sDelComm);
	
	$sDelCli = "DELETE FROM clienti_comm WHERE id_comm = ".intval(reqVar('id'));
	$objDB->exec($sDelCli);

	$sDelRich = "DELETE FROM richiedenti WHERE id_comm = ".intval(reqVar('id'));
	$objDB->exec($sDelRich);
	
	$sDelAtt = "DELETE FROM attivita WHERE id_comm = ".intval(reqVar('id'));
	$objDB->exec($sDelAtt);
	
	$sDelRint = "DELETE FROM rint WHERE id_comm = ".intval(reqVar('id'));
	$objDB->exec($sDelRint);
	
	$sDelAp = "DELETE FROM att_previste WHERE id_comm = ".intval(reqVar('id'));
	$objDB->exec($sDelAp);
	
	$sDelAss = "DELETE FROM assegnaz WHERE id_comm = ".intval(reqVar('id'));
	$objDB->exec($sDelAss);
	
	header("Location: ".urlTorna());
	exit();
}

?>